<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%>
<head>
    <script type="text/javascript">
       dojo.require("dojo.parser");
       dojo.require("dijit.dijit");
       dojo.require("dijit.form.Form");
       dojo.require("dijit.form.Button");
       dojo.require("dijit.form.TextBox");  
       dojo.require("dojox.layout.TableContainer"); 
       dojo.require("dojox.data.QueryReadStore"); 
       dojo.require("dojox.form.DateTextBox");     
       dojo.require("dijit.form.FilteringSelect");
       dojo.require("dojo.data.ItemFileReadStore");  
       dojo.require("dojox.grid.EnhancedGrid");
       dojo.require("dojox.grid.enhanced.plugins.DnD");
       dojo.require("dojox.grid.enhanced.plugins.Menu");
       dojo.require("dojox.grid.enhanced.plugins.NestedSorting");
       dojo.require("dojox.grid.enhanced.plugins.IndirectSelection");
       dojo.require("dojox.grid.Selection");
    </script>
</head>
<div dojoType="dojox.data.QueryReadStore" url="listCategory.htm" jsId="categoryComboStore"></div>
<!-- <div dojoType="dojox.data.QueryReadStore" url="listCategoryItems.htm" jsId="itemComboStore"></div>-->

<div dojoType="dijit.form.Form" id="dataForm" jsId="dataForm" encType="multipart/form-data"
 method="post">

 <div dojoType="dojox.layout.TableContainer" cols="2" id="tcCreateAuction" customClass="labelsAndValues">
    <div dojoType="dijit.form.TextBox" title="Auction Name:" id="name" name="description"></div>
    <div dojoType="dijit.form.TextBox" title="Summary:" id="summary" name="summary"></div>
    <div dojoType="dijit.form.TextBox" title="Start Time:" id="starttime" name="starttime"></div>
    <div dojoType="dijit.form.TextBox" title="End Time:" id="endtime" name="endtime"></div>
	<div dojoType="dojox.form.DateTextBox" title="Start Date:" id="startdate" name="startdate"></div>
	<div dojoType="dojox.form.DateTextBox" title="End Date:" id="enddate" name="enddate"></div>
	<div dojoType="dijit.form.ComboBox" title="Category:" id="categoryCombo" name="category" 
	jsId="categoryCombo" store="categoryComboStore" searchAttr="Description" pageSize="100"
    onChange="postCategory"></div>	
  </div> 
	
	<div dojoType="dojox.data.QueryReadStore" jsId="categoryItemsStore" url="listCategoryItems.htm"></div>
	<!-- A grid displays data and offer useful operations such as client sorting -->
	<div dojoType="dojox.grid.EnhancedGrid" id="gridCategoryItems" jsId="gridCategoryItems" 
	 structure="layout" store="categoryItemsStore"  
	 plugins="{nestedSorting: true, dnd: true, indirectSelection: true}" rowSelector="0px">
 	</div>
	<div dojoType="dijit.form.Button" id="moveToTrash">
			Move To Trash
	</div>
 <div type="dijit.form.Button" dojoType="dijit.form.Button" id="createAuctionSubmitButton" onClick="submitData()">Save</div>
 <div dojoType="dijit.form.Button" onClick="dijit.byId('loadDiv').hide();" id="createAuctionCancelButton">Cancel</div>
</div>

<script type="text/javascript">
	function postCategory(value){		
		var xhrArgs = {
				url : "listCategoryItems.htm",
				mimetype: "text/json",
				content : {"category": dojo.byId("categoryCombo").value},
				load: function(responseData) {
                    var storeData = dojo.fromJson(responseData);
                  	//dojo.byId("response").innerHTML = "Fetched category items." + responseData;                            
                    try{                            	
                    	var newStore = new dojo.data.ItemFileReadStore({data: storeData});  
                    	alert("newStore = "+newStore);                          	                            	
                    	gridCategoryItems.setStore(newStore);
                    }catch(e){
                         console.log(e);
                    }
                }
            }
            //dojo.byId("response").innerHTML = "Fetched auction items."
            //Call the asynchronous xhrPost
            var deferred = dojo.xhrPost(xhrArgs);
    }
	
	
	function submitData() {		
            var grid = dijit.byId('gridCategoryItems');
      		 alert("grid : "+grid);
      		 var items = dijit.byId('gridCategoryItems').selection.getSelected();
      		 alert("items = "+items);
               var selectedItems = new Array();
               if (items.length) {
                   // Iterate through the list of selected items.
                   // The current item is available in the variable
                   // "selectedItem" within the following function:
                   dojo.forEach(items, function(selectedItem) {
                       if (selectedItem !== null) {
                           // Iterate through the list of attributes of each item.
                           // The current attribute is available in the variable
                           // "attribute" within the following function:
                           dojo.forEach(grid.store.getAttributes(selectedItem), function(attribute) {                                	
                               // Get the value of the current attribute:
                               if (attribute == "Id") {                             
                               	var value = grid.store.getValues(selectedItem, attribute);
                               	selectedItems.push(value);
                               	alert(" ID :"+value);  
                               }                     
                           }); // end forEach
                       } // end if
                   }); // end forEach
               } // end if             
              
               alert("startdate = "+dojo.byId("startdate").value);
               alert("enddate = "+dojo.byId("enddate").value);               
          
               var xhrArgs = {
                       url : "saveAuction.htm",    
                   	   mimetype: "text/json",     
                       content: {"description" : dojo.byId("name").value,
                       		  "summary" : dojo.byId("summary").value,
                       		  "starttime" : dojo.byId("starttime").value,
                       		  "endtime" : dojo.byId("endtime").value,
                       		  "startdate" : dojo.byId("startdate").value,                	
                       		  "enddate" : dojo.byId("enddate").value,
                       		  "selectedIds" : selectedItems	
                   			 }
                   }
            //Call the asynchronous xhrPost
            //dojo.byId("response").innerHTML = "Form being sent..."
            var deferred = dojo.xhrPost(xhrArgs);
	}
</script>